//
//  LocationViewController.m
//  EasyBuy
//
//  Created by Zhu Xiangjun on 10/7/12.
//  Copyright (c) 2012 shen donghao. All rights reserved.
//

#import "LocationViewController.h"
#import "LocationTableViewCell.h"
#import "GoogleMapInfoData.h"
#import "GoogleMapViewController.h"

@interface LocationViewController ()

@end

@implementation LocationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    buttonSelected = YES;
    data = [[NSMutableArray alloc] init];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"TP link TL-R478+路由器",@"TP-服务器",@"TP-电脑", nil];
    longitudeArray = [[NSMutableArray alloc] initWithObjects:@"39.90960456049752",@"39.90960256049752",@"39.90960056049752", nil];
    latitudeArray = [[NSMutableArray alloc] initWithObjects:@"116.3972282409668",@"116.3972262409668",@"116.3972242409668", nil];
    NSLog(@"假设3个坐标");
    
    GoogleMapViewController *googleMapViewController = [[GoogleMapViewController alloc] init];
    googleMapViewController.view.frame = CGRectMake(0, 0, 320, 200);
    [self.view addSubview:googleMapViewController.view];
    [self setData];
    [googleMapViewController setBuyLocationInGoogleMap:data];
    
    
    UIButton *area = [UIButton buttonWithType:UIButtonTypeCustom];
    area.frame = CGRectMake(self.navigationController.navigationBar.center.x - 56.5,9, 113, 26);
    area.backgroundColor = [UIColor clearColor];
    [area addTarget:self action:@selector(changeArea:) forControlEvents:UIControlEventTouchUpInside];
    [area setTitle:@"附近500米" forState:UIControlStateNormal];
    [area setImage:nil forState:UIControlStateNormal];
    [area setBackgroundImage:[UIImage imageNamed:@"by500meterbg"] forState:UIControlStateSelected];
    [self.navigationController.navigationBar addSubview:area];
    
    [self setTableView];

    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    view.backgroundColor = [UIColor clearColor];
//    [self.view addSubview:view];
    
}

- (void)setData{
    
    int nCount = [dataArray count];
    for (int i = 0; i < nCount; ++ i) {
        GoogleMapInfoData *googleMapInfoData = [[GoogleMapInfoData alloc] init];
        googleMapInfoData.longitude = (double)[[longitudeArray objectAtIndex:i] doubleValue];
        googleMapInfoData.latitude = (double)[[latitudeArray objectAtIndex:i] doubleValue];
        googleMapInfoData.title = [dataArray objectAtIndex:i];
        [data addObject:googleMapInfoData];
        [googleMapInfoData release];
    }
    
}


- (void)changeArea:(id)sender{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 320, 480);
    [btn addTarget:sender action:@selector(removeButtons:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor grayColor];
    btn.alpha = 0.1;
    [self.view addSubview:btn];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"space500"]];
    imageView.frame = CGRectMake(self.navigationController.navigationBar.center.x - 68.5, 5, 137, 118);
    [self.view addSubview:imageView];
    [imageView release];

    
    UIButton *button500 = [UIButton buttonWithType:UIButtonTypeCustom];
    button500.frame = CGRectMake(self.navigationController.navigationBar.center.x - 68.5+9, 27, 120, 20);
    [button500 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button500 setTitle:@"附近500米" forState:UIControlStateNormal];
    [self.view addSubview:button500];
    
    UIButton *button1000 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1000.frame = CGRectMake(self.navigationController.navigationBar.center.x - 68.5+9, 61, 120, 20);
    [button1000 setTitle:@"附近1000米" forState:UIControlStateNormal];
    [button1000 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:button1000];
    
    UIButton *button3000 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3000.frame = CGRectMake(self.navigationController.navigationBar.center.x - 68.5+9, 94, 120, 20);
    [button3000 setTitle:@"附近3000米" forState:UIControlStateNormal];
    [button3000 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:button3000];
    
    

    if (buttonSelected) {
        UIButton *button = (UIButton *)sender;
        button.selected = !button.selected;
        [self.view addSubview:view];
        buttonSelected = NO;
    }else {
        UIButton *button = (UIButton *)sender;
        button.selected = !button.selected;
        [view removeFromSuperview];
        buttonSelected = YES;
        [view release];
    }
}

- (void)removeButtons:(id)sender{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    [view removeFromSuperview];
    buttonSelected = YES;
    [view release];
}

- (void)setTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, 320, self.view.frame.size.height - 200 -88) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    
    LocationTableViewCell *cell = (LocationTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    if (cell == nil) {
        cell = [[[LocationTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellInfoData:nil];

    [cell setCellInfoDataWithData:[dataArray objectAtIndex:indexPath.row]];
    return cell;
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
//    [googleMapViewController release];
    
    [super dealloc];
}

@end
